//道具
class Property{
    constructor(options,room){
        this.i=0;
        this.x=0;
        this.y=0;
        this.type=0;  //0代表泡泡 1代表能量药水  2代表鞋子  3代表图钉
        this.room=room;
        Object.assign(this,options);
        this.setTimer();
    }
    //设置定时消失
    setTimer(){
        this.timer=this.room.timerManager.setTimeout(function(){
            this.timer=null;
            if(!!this.room) {
                this.room.queue.pushTry(this.disappear.bind(this));
            }
        }.bind(this),10000);
    }
    getMessage(){
        return {
            i:this.i,
            x:this.x,
            y:this.y,
            type:this.type
        }
    }
    //吃道具
    eat(user){
        switch(this.type){
            case 0:{
                if(user.bombCount<10){
                    user.bombCount++;
                }
                break;
            }
            case 1:{
                if(user.power<10){
                    user.power++;
                }
                break;
            }
            case 2:{
                if(user.moveInterval>100){
                    user.moveInterval-=20;
                }
                break;
            }
            case 3:{
                user.thumbtackCount++;
                break;
            }
        }
        //讲该道具放入玩家吃掉的道具数组中，死亡时爆出来。
        user.properties.push(this);
        this.disappear(true,user.index);
    }
    //消失
    //isEat:是否被吃消失
    disappear(isEat=false,userIndex=0){
        if(!!this.timer){
            this.room.timerManager.clearTimeout(this.timer);
            this.timer=null;
        }
        let index=this.room.properties.findIndex(item=>{return item.i===this.i});
        if(index>=0){
            this.room.properties.splice(index,1);
            if(isEat){
                this.room.broadcastEvent('disappearProperty',{i:this.i,isEat:isEat,userIndex:userIndex});
            }else{
                this.room.broadcastEvent('disappearProperty',{i:this.i});
            }
        }
    }
}

module.exports=Property;